package main

import (
	"context"
	"fmt"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
)

/** 演示Watch机制 */

func main() {
	config, err := clientcmd.BuildConfigFromFlags("", "/home/cloudea/.kube/config")
	if err != nil {
		panic(err.Error())
	}

	client, err := kubernetes.NewForConfig(config)
	if err != nil {
		panic(err.Error())
	}

	w, err := client.AppsV1().Deployments("default").Watch(context.TODO(), metav1.ListOptions{})
	if err != nil {
		panic(err.Error())
	}
	fmt.Println("start...")
	for {
		select {
		case e, _ := <-w.ResultChan():
			fmt.Println(e.Type, e.Object)
			/*
				Added    EventType = "ADDED"
				Modified EventType = "MODIFIED"
				Deleted  EventType = "DELETED"
				Bookmark EventType = "BOOKMARK"
				Error    EventType = "ERROR"

			*/
			// e.Type: 变化类型: ADDED, MODIFIED, 等。modified会有多个阶段因此会回调多次
			// e.Object: 变化后的数据
		}
	}

}
